<template>
  <el-drawer :title="dialogTitle" v-model="dialogVisible"  size="70%">
    <el-form
      ref="formRef"
      :model="formData"
      :rules="formRules"
      label-width="150px"
      v-loading="formLoading"
    >
      <el-form-item label="应用类型" prop="appName">
        <el-input v-model="formData.appType" placeholder="请输入应用类型" />
      </el-form-item>
      <!--      <el-form-item label="应用类型" prop="appType">-->
      <!--        <el-select v-model="formData.appType" placeholder="请选择应用类型">-->
      <!--          <el-option label="请选择字典生成" value="" />-->
      <!--        </el-select>-->
      <!--      </el-form-item>-->
      <el-form-item label="appid" prop="appId">
        <el-input v-model="formData.appId" placeholder="请输入appid" />
      </el-form-item>
      <el-form-item label="应用名称" prop="appName">
        <el-input v-model="formData.appName" placeholder="请输入应用名称" />
      </el-form-item>
      <el-form-item label="定点医药机构编码" prop="orgCodg">
        <el-input v-model="formData.orgCodg" placeholder="请输入定点医药机构编码" />
      </el-form-item>
      <el-form-item label="渠道名称" prop="channelName">
        <el-input v-model="formData.channelName" placeholder="请输入第三方渠道名称" />
      </el-form-item>
      <el-form-item label="渠道号" prop="channel">
        <el-input v-model="formData.channel" placeholder="请输入渠道号" />
      </el-form-item>
      <el-form-item label="城市编码" prop="cityCode">
        <el-input v-model="formData.cityCode" placeholder="请输入城市编码" />
      </el-form-item>
      <el-form-item label="合作方id" prop="partnerId">
        <el-input v-model="formData.partnerId" placeholder="请输入合作方id" />
      </el-form-item>
      <el-form-item label="定点医药机构应用ID" prop="orgAppId">
        <el-input v-model="formData.orgAppId" placeholder="请输入定点医药机构应用ID" />
      </el-form-item>
      <el-form-item label="机构渠道认证编码" prop="orgChnlCrtfCodg">
        <el-input v-model="formData.orgChnlCrtfCodg" placeholder="请输入机构渠道认证编码" />
      </el-form-item>
      <el-form-item label="密钥" prop="partnerSecret">
        <el-input v-model="formData.partnerSecret" placeholder="请输入密钥" />
      </el-form-item>
      <el-form-item label="渠道授权地址" prop="channelOauthUrl">
        <el-input v-model="formData.channelOauthUrl" placeholder="请输入渠道授权地址" />
      </el-form-item>
      <el-form-item label="渠道获取用户地址" prop="channelUserUrl">
        <el-input v-model="formData.channelUserUrl" placeholder="请输入渠道获取用户地址" />
      </el-form-item>
      <el-form-item label="回调地址" prop="redirectUrl">
        <el-input v-model="formData.redirectUrl" placeholder="请输入回调地址" />
      </el-form-item>
      <el-form-item label="his分账下单地址" prop="hisOrderUrl">
        <el-input v-model="formData.hisOrderUrl" placeholder="请输入his分账下单地址" />
      </el-form-item>
      <el-form-item label="his支付确认地址" prop="hisConfirmUrl">
        <el-input v-model="formData.hisConfirmUrl" placeholder="请输入his支付确认地址" />
      </el-form-item>
      <el-form-item label="his退费地址" prop="hisRefundUrl">
        <el-input v-model="formData.hisRefundUrl" placeholder="请输入his退费地址" />
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-radio-group v-model="formData.status">
          <el-radio
            v-for="dict in getIntDictOptions(DICT_TYPE.COMMON_STATUS)"
            :key="dict.value"
            :label="dict.value"
          >
            {{ dict.label }}
          </el-radio>
        </el-radio-group>
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="submitForm" type="primary" :disabled="formLoading">确 定</el-button>
      <el-button @click="dialogVisible = false">取 消</el-button>
    </template>
  </el-drawer>
</template>
<script setup lang="ts">
import { getIntDictOptions, DICT_TYPE } from '@/utils/dict'
import { ChsConfigApi, ChsConfigVO } from '@/api/hospital/chs/config'

/** 医保支付配置 表单 */
defineOptions({ name: 'ChsConfigForm' })

const { t } = useI18n() // 国际化
const message = useMessage() // 消息弹窗

const dialogVisible = ref(false) // 弹窗的是否展示
const dialogTitle = ref('') // 弹窗的标题
const formLoading = ref(false) // 表单的加载中：1）修改时的数据加载；2）提交的按钮禁用
const formType = ref('') // 表单的类型：create - 新增；update - 修改
const formData = ref({
  id: undefined,
  appId: undefined,
  appName: undefined,
  appType: undefined,
  orgAppId: undefined,
  orgChnlCrtfCodg: undefined,
  channelName: undefined,
  channel: undefined,
  cityCode: undefined,
  orgCodg: undefined,
  partnerId: undefined,
  partnerSecret: undefined,
  channelOauthUrl: undefined,
  channelUserUrl: undefined,
  redirectUrl: undefined,
  hisOrderUrl: undefined,
  hisConfirmUrl: undefined,
  hisRefundUrl: undefined,
  status: undefined
})
const formRules = reactive({
  appId: [{ required: true, message: 'appid不能为空', trigger: 'blur' }],
  appName: [{ required: true, message: '应用名称不能为空', trigger: 'blur' }],
  appType: [{ required: true, message: '应用类型不能为空', trigger: 'change' }],
  orgAppId: [{ required: true, message: '定点医药机构应用ID不能为空', trigger: 'blur' }],
  orgChnlCrtfCodg: [{ required: true, message: '机构渠道认证编码不能为空', trigger: 'blur' }],
  channelName: [{ required: true, message: '第三方渠道名称不能为空', trigger: 'blur' }],
  channel: [{ required: true, message: '渠道号不能为空', trigger: 'blur' }],
  cityCode: [{ required: true, message: '城市编码不能为空', trigger: 'blur' }],
  orgCodg: [{ required: true, message: '定点医药机构编码不能为空', trigger: 'blur' }],
  partnerId: [{ required: true, message: '合作方id不能为空', trigger: 'blur' }],
  partnerSecret: [{ required: true, message: '密钥不能为空', trigger: 'blur' }],
  channelOauthUrl: [{ required: true, message: '渠道授权地址不能为空', trigger: 'blur' }],
  channelUserUrl: [{ required: true, message: '渠道获取用户地址不能为空', trigger: 'blur' }],
  status: [{ required: true, message: '状态不能为空', trigger: 'blur' }]
})
const formRef = ref() // 表单 Ref

/** 打开弹窗 */
const open = async (type: string, id?: number) => {
  dialogVisible.value = true
  dialogTitle.value = t('action.' + type)
  formType.value = type
  resetForm()
  // 修改时，设置数据
  if (id) {
    formLoading.value = true
    try {
      formData.value = await ChsConfigApi.getChsConfig(id)
    } finally {
      formLoading.value = false
    }
  }
}
defineExpose({ open }) // 提供 open 方法，用于打开弹窗

/** 提交表单 */
const emit = defineEmits(['success']) // 定义 success 事件，用于操作成功后的回调
const submitForm = async () => {
  // 校验表单
  await formRef.value.validate()
  // 提交请求
  formLoading.value = true
  try {
    const data = formData.value as unknown as ChsConfigVO
    if (formType.value === 'create') {
      await ChsConfigApi.createChsConfig(data)
      message.success(t('common.createSuccess'))
    } else {
      await ChsConfigApi.updateChsConfig(data)
      message.success(t('common.updateSuccess'))
    }
    dialogVisible.value = false
    // 发送操作成功的事件
    emit('success')
  } finally {
    formLoading.value = false
  }
}

/** 重置表单 */
const resetForm = () => {
  formData.value = {
    id: undefined,
    appId: undefined,
    appName: undefined,
    appType: undefined,
    orgAppId: undefined,
    orgChnlCrtfCodg: undefined,
    channelName: undefined,
    channel: undefined,
    cityCode: undefined,
    orgCodg: undefined,
    partnerId: undefined,
    partnerSecret: undefined,
    channelOauthUrl: undefined,
    channelUserUrl: undefined,
    redirectUrl: undefined,
    hisOrderUrl: undefined,
    hisConfirmUrl: undefined,
    hisRefundUrl: undefined,
    status: undefined
  }
  formRef.value?.resetFields()
}
</script>
